package com.changgou.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //添加积分任务交换机
    public static final String EX_BUYING_ADDPOINTUSER = "ex_buying_addpointuser";

    //添加积分消息队列
    public static final String CG_BUYING_ADDPOINT = "cg_buying_addpoint";

    //完成添加积分消息队列
    public static final String CG_BUYING_FINISHADDPOINT = "cg_buying_finishaddpoint";

    //添加积分路由key
    public static final String CG_BUYING_ADDPOINT_KEY = "addpoint";

    //完成添加积分路由key
    public static final String CG_BUYING_FINISHADDPOINT_KEY = "finishaddpoint";

    private static final String ORDER_PAY="order_pay";

    public static final String ORDER_TASK = "order_task" ;

    //声明交换机
    @Bean(EX_BUYING_ADDPOINTUSER)
    public Exchange EX_BUYING_ADDPOINTUSER(){
        return ExchangeBuilder.directExchange(EX_BUYING_ADDPOINTUSER).durable(true).build();
    }

    //声明队列
    @Bean(CG_BUYING_ADDPOINT)
    public Queue CG_BUYING_ADDPOINT(){
        return new Queue(CG_BUYING_ADDPOINT);
    }

    @Bean(CG_BUYING_FINISHADDPOINT)
    public Queue CG_BUYING_FINISHADDPOINT(){
        return new Queue(CG_BUYING_FINISHADDPOINT);
    }

    //绑定交换机
    @Bean
    public Binding BINDING_CG_BUYING_ADDPOINT(@Qualifier(EX_BUYING_ADDPOINTUSER)Exchange exchange,@Qualifier(CG_BUYING_ADDPOINT)Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_ADDPOINT_KEY).noargs();
    }

    @Bean
    public Binding BINDING_CG_BUYING_FINISHADDPOINT(@Qualifier(EX_BUYING_ADDPOINTUSER)Exchange exchange,@Qualifier(CG_BUYING_FINISHADDPOINT)Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(CG_BUYING_FINISHADDPOINT_KEY).noargs();
    }

    @Bean
    public Queue order_pay(){
        return QueueBuilder.durable(ORDER_PAY).build();
    }

    @Bean(ORDER_TASK)
    public Queue ORDER_TASK(){
        return new Queue(ORDER_TASK);
    }


    //订单超时状态交换机
    public static final String EX_ORDER_PAY_OVERTIME = "ex_order_pay_overtime";

    //订单超时状态过期队列
    public static final String QUEUE_ORDER_PAY_OVERTIME_TTL = "queue_order_pay_overtime_ttl";

    //订单超时状态交换机
    public static final String DLX_ORDER_PAY_OVERTIME = "dlx_order_pay_overtime";

    //订单超时状态队列
    public static final String QUEUE_ORDER_PAY_OVERTIME = "queue_order_pay_overtime";

    /**
     * 支付状态检查交换机
     * @return
     */
    @Bean(EX_ORDER_PAY_OVERTIME)
    public Exchange EX_ORDER_PAY_OVERTIME(){
        return ExchangeBuilder.fanoutExchange(EX_ORDER_PAY_OVERTIME).durable(true).build();
    }

    /**
     * 支付状态检查过期队列
     * @return
     */
    @Bean(QUEUE_ORDER_PAY_OVERTIME_TTL)
    public Queue QUEUE_ORDER_PAY_OVERTIME_TTL(){
        return QueueBuilder.durable(QUEUE_ORDER_PAY_OVERTIME_TTL)
                .withArgument("x-message-ttl",300000) //设置队列消息过期时间
                .withArgument("x-dead-letter-exchange",DLX_ORDER_PAY_OVERTIME) //设置过期队列绑定的死信交换机
                .build();
    }

    /**
     * 正常交换机绑定过期队列
     * @param exchange
     * @param queue
     * @return
     */
    @Bean
    public Binding BINDING_ORDER_PAY_OVERTIME_TTL(@Qualifier(EX_ORDER_PAY_OVERTIME) Exchange exchange ,@Qualifier(QUEUE_ORDER_PAY_OVERTIME_TTL) Queue queue){
        return BindingBuilder.bind(queue).to((FanoutExchange)exchange);
    }

    /**
     * 支付状态检查死信交换机
     * @return
     */
    @Bean(DLX_ORDER_PAY_OVERTIME)
    public Exchange DLX_ORDER_PAY_OVERTIME(){
        return ExchangeBuilder.fanoutExchange(DLX_ORDER_PAY_OVERTIME).durable(true).build();
    }

    /**
     * 支付状态检查正常队列
     * @return
     */
    @Bean(QUEUE_ORDER_PAY_OVERTIME)
    public Queue QUEUE_ORDER_PAY_OVERTIME(){
        return QueueBuilder.durable(QUEUE_ORDER_PAY_OVERTIME).build();
    }

    /**
     * 正常交换机绑定过期队列
     * @param exchange
     * @param queue
     * @return
     */
    @Bean
    public Binding BINDING_DLX_ORDER_PAY_OVERTIME_TTL(@Qualifier(DLX_ORDER_PAY_OVERTIME) Exchange exchange ,@Qualifier(QUEUE_ORDER_PAY_OVERTIME) Queue queue){
        return BindingBuilder.bind(queue).to((FanoutExchange) exchange);
    }
}
